// import { Message } from 'element-ui' //暂时用element-ui的alert样式
import axios from 'axios'
import Vue from 'vue'
import qs from 'qs'
import Cookies from 'js-cookie' 
// import Router from '@/router' //用于个别页面处理
var merge = require('webpack-merge')

axios.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest"
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; 
axios.interceptors.request.use(
  config =>{
    if(config.method  === 'post'){
      //
      config.data = qs.stringify(config.data);
    }
    if(Cookies.get('token')){
      // 获取本地Cookies内容

    }
    //#region
    // var from = (window.zhaopinGlobal && window.zhaopinGlobal.from) ? window.zhaopinGlobal.from.path : "";
    // if (from == null || from == undefined || from == '') from = document.referrer;
    //加入每一个请求时，后台需要的一些，前端可以得到的Header内容
    //config.headers.common['URL'] = 'URL'
    // config.headers.common['CategoryName'] = 'ResumeMobile'  //类别
    // config.headers.common['zpReferer'] = encodeURI(from)    //页面来源
    // config.headers.common['URL'] = document.location.href   //当前页面地址
    // config.headers.common['UserAgent'] = navigator.userAgent
    //#endregion

    return config
  },
  err => {
    return Promise.reject(err);
  }
);


axios.interceptors.response.use(
  response => {
    //错误信息统一处理
    return response
  },
  error => {
    return Promise.reject(error)
  }
);

export default {
  install(Vue) {
      Vue.prototype.$zpHttp = axios
      Vue.zpHttp = axios
  },
  // $zpHttp: axios
}

export const $zpHttp = merge(axios);